package com.fanfan.service.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fanfan.dao.OrderDao;
import com.fanfan.pojo.Order;
import com.fanfan.pojo.domain.ResponseResult;
import com.fanfan.pojo.vo.OrderVo;
import com.fanfan.pojo.vo.PageVo;
import com.fanfan.service.OrderService;
import com.fanfan.service.TradeService;
import com.fanfan.service.UserService;
import com.fanfan.utils.BeanCopyUtils;
import com.fanfan.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * (Order)表服务实现类
 *
 * @author fansea
 * @since 2023-03-13 12:44:52
 */
@Service("orderService")
public class OrderServiceImpl extends ServiceImpl<OrderDao, Order> implements OrderService {
    @Autowired
    TradeService tradeService;

    @Override
    public ResponseResult orderList(Integer pageNum, Integer pageSize) {
        System.out.println("SecurityUtils.getUserId()="+SecurityUtils.getUserId());
        LambdaQueryWrapper<Order> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.eq(Objects.nonNull(SecurityUtils.getUserId()),Order::getCreateBy, SecurityUtils.getUserId());
        queryWrapper.orderByAsc(Order::getStatus);
        Page<Order> page = new Page<>();
        page.setCurrent(pageNum);//设置第几页
        page.setSize(pageSize);//设置一页多少个
        page(page, queryWrapper);
        /*page.getRecords().stream()
                .map(order -> order.setThumbnail(tradeService.getById(order.getTradeId()).getThumbnail()))
                .map(order -> order.set)*/
        List<OrderVo> orderVos = BeanCopyUtils.copyList(page.getRecords(), OrderVo.class);
        List<OrderVo> collect = orderVos.stream()
                .map(order -> order.setThumbnail(tradeService.getById(order.getTradeId()).getThumbnail()))
                .map(order -> order.setTradeName(tradeService.getById(order.getTradeId()).getTradeName()))
                .collect(Collectors.toList());
        PageVo pageVo = new PageVo(collect,page.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult listOrder(Integer pageNum, Integer pageSize, Order order) {
        LambdaQueryWrapper<Order> queryWrapper=new LambdaQueryWrapper();
        System.out.println("收件人"+order.getAddressee());
        queryWrapper.like(Objects.nonNull(order.getAddressee()),Order::getAddressee, order.getAddressee());
        queryWrapper.eq(Objects.nonNull(order.getStatus()),Order::getStatus,order.getStatus());
        queryWrapper.eq(Objects.nonNull(order.getRegion()),Order::getRegion,order.getRegion());
        Page<Order> page = new Page<>();
        page.setCurrent(pageNum);//设置第几页
        page.setSize(pageSize);//设置一页多少个
        page(page, queryWrapper);
        /*page.getRecords().stream()
                .map(order -> order.setThumbnail(tradeService.getById(order.getTradeId()).getThumbnail()))
                .map(order -> order.set)*/
        List<OrderVo> orderVos = BeanCopyUtils.copyList(page.getRecords(), OrderVo.class);
        List<OrderVo> collect = orderVos.stream()
                .map(orders -> orders.setThumbnail(tradeService.getById(orders.getTradeId()).getThumbnail()))
                .map(orders -> orders.setTradeName(tradeService.getById(orders.getTradeId()).getTradeName()))
                .collect(Collectors.toList());
        PageVo pageVo = new PageVo(collect,page.getTotal());
        return ResponseResult.okResult(pageVo);
    }
}

